package com.czy.owner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.SelectBespokeListModel;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wangyang on 2017/5/18 0018.
 */

public class SelectBespokeAdapter extends RecyclerArrayAdapter<SelectBespokeListModel> {

    private Context context;
    LinkedHashMap<String, SelectBespokeListModel> map = new LinkedHashMap<String, SelectBespokeListModel>();
    OnDataListener onDataListener;
    private List<SelectBespokeListModel> selectedList = new ArrayList<>();

    public SelectBespokeAdapter(Context context) {
        super(context);
        this.context = context;
    }

    public void setOnDataListener(OnDataListener onDataListener) {
        this.onDataListener = onDataListener;
    }

    public void setSelectedList(List<SelectBespokeListModel> list) {
        map.clear();
        for (SelectBespokeListModel bespokeListModel : list) {
            map.put(bespokeListModel.getStoreServiceId(), bespokeListModel);
        }
    }

    public List<SelectBespokeListModel> getSelectedList() {
        selectedList.clear();

        for (Map.Entry<String, SelectBespokeListModel> entry : map.entrySet()) {
            selectedList.add(entry.getValue());
        }
        return selectedList;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderViewHolder(parent);
    }

    class OrderViewHolder extends BaseViewHolder<SelectBespokeListModel> {

        @BindView(R.id.tv_select_car)
        TextView tvSelectCar;
        @BindView(R.id.rb_statistics_project)
        ToggleButton rbStatisticsProject;
        @BindView(R.id.img_logo)
        ImageView imgLogo;
        @BindView(R.id.tv_details)
        TextView tvDetails;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_deposit)
        TextView tvDeposit;
        @BindView(R.id.tv_mem_price)
        TextView tvMemPrice;
        @BindView(R.id.linear_select_project)
        LinearLayout linearSelectProject;

        public OrderViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_select_service);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(final SelectBespokeListModel data) {
            super.setData(data);
            tvSelectCar.setText(data.getServiceName());
            rbStatisticsProject.setChecked(map.containsKey(data.getStoreServiceId()));
            tvDetails.setText(data.getDescription());
            tvMoney.setText("¥" + DecimalUtil.formatNum(data.getServicePrice()));
            tvDeposit.setText("¥" + DecimalUtil.formatNum(data.getReservationPrice()));
            if(TextUtils.isEmpty(data.getStoreMemberPrice())){
                tvMemPrice.setVisibility(View.GONE);
            }else {
                tvMemPrice.setVisibility(View.VISIBLE);
                tvMemPrice.setText("￥" + DecimalUtil.formatNum(data.getStoreMemberPrice()));
            }
            if (Double.parseDouble(StringUtils.getString(data.getReservationPrice(),"0"))==0){
                tvDeposit.setVisibility(View.GONE);
            }else {
                tvDeposit.setVisibility(View.VISIBLE);
                tvDeposit.setText("¥" + DecimalUtil.formatNum(data.getReservationPrice()));
            }

            //添加数据
            GlideUtils.loadImage(context, data.getLogo(), imgLogo, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }
                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_bespoke_service, R.mipmap.default_bespoke_service);

            linearSelectProject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (map.containsKey(data.getStoreServiceId())) {
                        map.remove(data.getStoreServiceId());
                    } else {
                        map.put(data.getStoreServiceId(), data);
                    }
                    notifyDataSetChanged();

                }
            });
        }
    }

    public interface OnDataListener {
        void onData(SelectBespokeListModel data);
    }
}